package com.itwh.project.service.impl;

import com.itwh.project.common.ResultObj;
import com.itwh.project.domain.Course;
import com.itwh.project.domain.LabLaboratoryCourse;
import com.itwh.project.mapper.LabLaboratoryCourseMapper;
import com.itwh.project.service.LabLaboratoryCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * <p>
 * 实验室-课程：本实验室可以完成哪些课程的实验 服务实现类
 * </p>
 *
 * @author 翁煌
 * @since 2020-07-05
 */
@Service
public class LabLaboratoryCourseServiceImpl extends ServiceImpl<LabLaboratoryCourseMapper, LabLaboratoryCourse> implements LabLaboratoryCourseService {

    @Autowired
    private LabLaboratoryCourseMapper labLaboratoryCourseMapper;

    @Override
    public List<Long> findCourseIdByLabId(Long lab_id) {

        return labLaboratoryCourseMapper.findCourseIdByLabId(lab_id);

    }

    @Override
    public ResultObj addCourseId(Long college_id, Long lab_id, Long cid, Long by_who, Boolean deleted, Long last_version) {

        try {
            //先查找是否存在这门课
            Course course = labLaboratoryCourseMapper.selectLabCourse(lab_id, cid);
            //如果存在并且deleted为false，那就会增加失败；如果存在并且deleted为true，那就会增加成功，修改deleted为false；如果不存在，则添加课程在这间实验室
            if (course!=null&&course.getDeleted()==false){
                return ResultObj.ADD_ERROR2;
            }else if (course!=null&&course.getDeleted()==false){
                labLaboratoryCourseMapper.updateDeletedByLabIdAndCId2(lab_id,cid);
                return ResultObj.ADD_SUCCESS;
            }else {
                labLaboratoryCourseMapper.saveCourseId(college_id,lab_id,cid,by_who,deleted,last_version);
                return ResultObj.ADD_SUCCESS;
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }


    @Override
    public ResultObj updateDeletedByLabIdAndCId(Long lab_id,Long cid) {
        try {
            //修改deleted为false
            labLaboratoryCourseMapper.updateDeletedByLabIdAndCId(lab_id,cid);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


}
